﻿namespace OleDbDAL.Systems
{
    using DBUtility;
    using IDal.Systems;
    using Model.Systems;
    using System;
    using System.Data;
    using System.Text;

    public class ParameterApp : IParameter
    {
        private string dbname = "Systems";

        public void Add(ParameterEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_Parameter(");
            builder.Append("ParFId,ParName,ParValue,ParSpell,ParRefName,ParAddTime,ClassDepth,ParIdList,IsSystemPar,AutomaticNumber");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.ParFId + ",");
            builder.Append("'" + model.ParName + "',");
            builder.Append("'" + model.ParValue + "',");
            builder.Append("'" + model.ParSpell + "',");
            builder.Append("'" + model.ParRefName + "',");
            builder.Append("'" + model.ParAddTime + "',");
            builder.Append("" + model.ClassDepth + ",");
            builder.Append("'" + model.ParIdList + "',");
            builder.Append("" + model.IsSystemPar + ",");
            builder.Append("" + model.AutomaticNumber + "");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int ParId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_Parameter ");
            builder.Append(" where ParId=" + ParId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Parameter ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by ParId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "ParId", "T_Parameter", swhere);
        }

        public ParameterEnt GetModel(int ParId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ParId,ParFId,ParName,ParValue,ParSpell,ParRefName,ParAddTime,ClassDepth,ParIdList,IsSystemPar,AutomaticNumber from T_Parameter ");
            builder.Append(" where ParId=" + ParId);
            ParameterEnt ent = new ParameterEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.ParId = ParId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["ParFId"].ToString() != "")
                {
                    ent.ParFId = int.Parse(table.Rows[0]["ParFId"].ToString());
                }
                ent.ParName = table.Rows[0]["ParName"].ToString();
                ent.ParValue = table.Rows[0]["ParValue"].ToString();
                ent.ParSpell = table.Rows[0]["ParSpell"].ToString();
                ent.ParRefName = table.Rows[0]["ParRefName"].ToString();
                if (table.Rows[0]["ParAddTime"].ToString() != "")
                {
                    ent.ParAddTime = DateTime.Parse(table.Rows[0]["ParAddTime"].ToString());
                }
                if (table.Rows[0]["ClassDepth"].ToString() != "")
                {
                    ent.ClassDepth = int.Parse(table.Rows[0]["ClassDepth"].ToString());
                }
                ent.ParIdList = table.Rows[0]["ParIdList"].ToString();
                if (table.Rows[0]["IsSystemPar"].ToString() != "")
                {
                    ent.IsSystemPar = int.Parse(table.Rows[0]["IsSystemPar"].ToString());
                }
                if (table.Rows[0]["AutomaticNumber"].ToString() != "")
                {
                    ent.AutomaticNumber = int.Parse(table.Rows[0]["AutomaticNumber"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Parameter ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "ParId");
        }

        public void Update(ParameterEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_Parameter set ");
            builder.Append("ParFId=" + model.ParFId + ",");
            builder.Append("ParName='" + model.ParName + "',");
            builder.Append("ParValue='" + model.ParValue + "',");
            builder.Append("ParSpell='" + model.ParSpell + "',");
            builder.Append("ParRefName='" + model.ParRefName + "',");
            builder.Append("ParAddTime='" + model.ParAddTime + "',");
            builder.Append("ClassDepth=" + model.ClassDepth + ",");
            builder.Append("ParIdList='" + model.ParIdList + "',");
            builder.Append("IsSystemPar=" + model.IsSystemPar + ",");
            builder.Append("AutomaticNumber=" + model.AutomaticNumber + "");
            builder.Append(" where ParId=" + model.ParId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

